import { getAllGoods } from '../services/goodsService';
import { addGoods, deleteGoods, getGoodsType,changeGoods } from '../services/goodsService';
export default {
    namespaced: true,
    state: {
        // isLoading: true,//加载中
        goods: [],//所有商品
        page: 1,//当前页
        limit: 3,//一页显示多少条数据
        pagerCount: 9,//当前显示多少页码
        total:null,//总条数

    },
    //同步函数
    mutations: {
        setGoods(state, payload) {//商品
            state.goods = payload;
        },
        
        // //删除
        // setNewGoods(state, payload) {
        //     state.goods = state.goods.filter(item => item._id != payload);//返回一个新数组，删掉不要的id
        // },
        // //新增
        // setAddGoods(state, payload) {
        //     state.goods = state.goods.push(payload);

        // },
        setLoading(state, payload) {//加载
            state.isLoading = payload;
        },
        //分页
        setTotal(state, payload) {
            state.total = payload;
        }

    },
    //异步
    actions: {
        //所有商品
        async getGoodsData(context,payload) {
            if (payload) {
                let { page, pageSize } = payload;
                // context.commit("setLoading", true);
                let resp = await getAllGoods(page, pageSize);
                console.log(resp);
                context.commit("setGoods", resp.data.rows);
                context.commit("setTotal", resp.data.total);
                // context.commit("setLoading", false);
                // return resp;
            }
            else {
                // context.commit(" setLoading", true);
                let resp = await getAllGoods(context.state.page, context.state.limit);
                console.log(resp);
                context.commit("setGoods", resp.data.rows);
                context.commit("setTotal", resp.data.total);
                // context.commit("setLoading", false);
            }
            // context.commit("setLoading", true);
            // let resp = await getAllGoods();
            // console.log(resp);
            // context.commit("setGoods", resp.data.rows);
            // context.commit("setLoading", false);
            // return resp;
        },
        //新增
        async addGoodsData(context, payload) {
            // context.commit("setLoading", true);
            await addGoods(payload);//从组件那边传过来的新增对象
            // context.commit("setLoading", false);
        },
        //删除
        async deleteGoodsData(context, payload) {
            await deleteGoods(payload);
            // context.commit("setNewGoods", payload);
        },
        //修改
        async changeGoodsData(context, payload) { 
            console.log(payload);
            let { id, name, price, description, tag } = payload
            await changeGoods(id, name, price, description, tag)
        },
        //分类查询
        async getInputGoods(context, payload) {
            let resp = await getGoodsType(payload);
            console.log(resp);
            context.commit('setGoods', resp.data.rows);//把删选的数据重新渲染到表格   
        },
        //搜索查询
        async inputGoods(context, payload) {
            console.log(payload);
            let resp = await getGoodsType(payload);
            console.log(resp);//
        },
    }
}